package com.qiqi.admin.basicdata.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.qiqi.basicdata.dto.CtpDTO;
import com.qiqi.basicdata.entity.CtpDO;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiqi.common.entity.PageEntity;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qiqi.basicdata.service.CtpService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiQiDream
 * @since 2020-11-27
 */
@Api("相关接口")
@RestController
@RequestMapping("/ctp")
public class CtpController {

    @Resource
    private CtpService ctpService;

    @ApiOperation(value = "获取(列表)")
    @PostMapping("/list")
    public PageEntity<CtpDO> getCtpPage(@RequestBody CtpDTO ctp) {
        LambdaQueryWrapper<CtpDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(ctp.getCode()),CtpDO::getCode,ctp.getCode())
                .like(StringUtils.isNotBlank(ctp.getName()),CtpDO::getName,ctp.getName());
        IPage<CtpDO> iPage = ctpService.page(new Page<>(ctp.getPage(),ctp.getCount()),queryWrapper);
        return new PageEntity<>(iPage.getTotal(),Convert.convert(new TypeReference<List<CtpDO>>() {}, iPage.getRecords()));
    }

    @ApiOperation(value = "获取(下拉框)")
    @GetMapping("/all")
    public List<CtpDO> getCtpList() {
        return ctpService.list();
    }

    @ApiOperation(value = "获取(单个)")
    @GetMapping("/{id}")
    public CtpDO getCtp(@PathVariable Long id) {
        return ctpService.getById(id);
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public Boolean saveCtp(@RequestBody CtpDO ctpDO) {
        return ctpService.saveOrUpdate(ctpDO);
    }

    @ApiOperation(value = "删除(批量))")
    @DeleteMapping("")
    public Boolean deleteCtpByIds(@RequestBody List<Long> idList) {
        return ctpService.removeByIds(idList);
    }

    @ApiOperation(value = "删除(单个))")
    @DeleteMapping("/{id}")
    public Boolean deleteCtpById(@PathVariable Long id) {
        return ctpService.removeById(id);
    }
}
